import {Injectable} from '@angular/core';
import {Observable} from 'rxjs/Observable';

import {HttpProvider} from "@providers/http";
import {StorageProvider} from "@providers/storage";
import {DialogProvider} from "@providers/dialog";


/*
 * 查询分页服务
 */
@Injectable()
export class PagingProvider {
  userInfo: any = this.storage.read("userInfo") || {};

  constructor(private http: HttpProvider,
              private storage: StorageProvider,
              private dialog: DialogProvider) {
  }

  get(params: QueryParams, config: PagingConfig): Observable<any> {
    config = config || {};
    params.url = params.url || '/Inbound/invoke.do';
    params.data = params.data || {};
    params.data["skip"] = config.skip;
    params.data["limit"] = config.limit;
    let observable = this.http.post(params.url, {
      interfaceId: params.interfaceId,
      data: JSON.stringify(params.data)
    });
    return Observable.create((observer) => {
      observable.subscribe(message => {
          let data = [];
          if (typeof message == 'string') {
            data = JSON.parse(message).data;
          } else {
            data = message.data || [];
          }
          config.skip += data.length;
          if (data.length < config.limit) {
            config.isEnd = true;
          }
          observer.next(data);
          observer.complete();
        },
        error => {
          observer.error(error);
          this.dialog.showToast(error);
          observer.complete();
        });
    });
  }

  query(params: Object, config?: PagingConfig): Observable<any> {
    config = config || {};
    var url = params["url"] || '/query/api.do';
    let data: object = {};

    //验证
    let userInfo: any = this.storage.read("userInfo") || {};
    data["SessionID"] = userInfo["SessionID"]; //查询条件

    //传入参数
    //data["query_bean"] = sqlMap.query_bean; //查询javaBean
    var query_data = {};
    Object.assign(query_data, params["query_data"], userInfo);
    data["query_inputdata"] = JSON.stringify(query_data); //查询条件

    //默认参数
    data["response_compression"] = "gzip"; //返回数据压缩
    data["query_extend_access"] = true; //启用权限
    data["query_access_user"] = userInfo["PCRM_CZY02"]; //账号
    data["query_access_company"] = userInfo["PCRM_GSXX01"]; //公司
    data["query_datasource"] = "scm"; //数据源
    data["response_data_format"] = "json";

    data["response_page_size"] = config.limit || 999999999; //grid.queryApi.page_size; //每页数量

    data["response_speed_priority"] = true;
    data["response_page_num"] = config.skip || 1; //第几页数据

    delete params["data"];
    Object.assign(data, params);

    let observable = this.http.get(url, data);
    return Observable.create((observer) => {
      observable.subscribe(message => {
          var data: Array<Object> = [];
          if (params["query_bean"] == "mongoQuery") {
            data = message;
          } else {
            data = message.slice(0, message.length - 1);
          }

          config.skip++;
          if (data.length < config.limit) {
            config.isEnd = true;
          }
          observer.next(data);
          observer.complete();
        },
        error => {
          observer.error(error);
          this.dialog.showToast(error);
        });
    });
  }
}

export class PagingConfig {
  skip?: number = 0;  //起始行
  limit?: number = 10; //页大小
  isEnd?: boolean = false; //是否最后一页
}

export class QueryParams {
  url?: string;
  interfaceId: string;
  data?: any;
}
